package cn.jingsheng.backup.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.jingsheng.backup.bean.UserLogin;
import cn.jingsheng.backup.util.DbUtil;

/**
 * 用户角色操作类
 * 
 * @author 李晓丹
 * 
 */
public class UserLoginDao {
	// 日志记录类
	Log log = LogFactory.getLog(UserLoginDao.class);

	/**
	 * 校验用户登录
	 * 
	 * @param user
	 * @param pass
	 * @return
	 */
	public UserLogin login(String user, String pass) {
		UserLogin userLonin = null;
		PreparedStatement ps = DbUtil.prepareStatement("select * from user_login where user_name = ? and password = ?", user, pass);
		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
			if (rs.next()) {
				userLonin = new UserLogin();
				userLonin.setUserName(rs.getString("user_name"));
				userLonin.setRoleId(rs.getInt("role_id"));
				userLonin.setPassword(rs.getString("password"));
			}
			return userLonin;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
			DbUtil.close(rs);
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param userLogin
	 * @param newPassword
	 * @return
	 */
	public int changePassword(UserLogin userLogin, String newPassword) {
		PreparedStatement ps = DbUtil.prepareStatement("update user_login set password=? where user_name = ? and password = ?", newPassword, userLogin.getUserName(), userLogin.getPassword());
		try {
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}

	/**
	 * 获取所有用户列表
	 * 
	 * @return
	 */
	public List<UserLogin> queryAll() {
		List<UserLogin> list = new ArrayList<UserLogin>();
		PreparedStatement ps = DbUtil.prepareStatement("select * from user_login ");
		ResultSet rs = null;
		try {
			rs = ps.executeQuery();

			while (rs.next()) {
				UserLogin userLonin = new UserLogin();
				userLonin.setUserName(rs.getString("user_name"));
				userLonin.setRoleId(rs.getInt("role_id"));
				userLonin.setPassword(rs.getString("password"));
				list.add(userLonin);
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
			DbUtil.close(rs);
		}
		return list;
	}

	/**
	 * 根据用户名删除用户
	 * 
	 * @param userLogin
	 * @param newPassword
	 * @return
	 */
	public int deleteUserByName(String username) {
		PreparedStatement ps = DbUtil.prepareStatement("delete user_login where user_name = ?", username);
		try {
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}
	/**
	 * 新增用户
	 * 
	 * @param userLogin
	 * @param newPassword
	 * @return
	 */
	public int insert(UserLogin user) {
		PreparedStatement ps = DbUtil.prepareStatement("insert into user_login(user_name,password,role_id) values(?,?,?)", user.getUserName(),user.getPassword(),user.getRoleId());
		try {
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}

	/**
	 * 批量更新用户权限
	 * 
	 * @param users
	 */
	public void updateRoleIdByBatch(List<UserLogin> users) {
		PreparedStatement ps = DbUtil.prepareStatement("update user_login set role_id = ? where user_name = ?");
		try {
			for (int i = 0; users != null && i != users.size(); i++) {
				ps.setInt(1, users.get(i).getRoleId());
				ps.setString(2, users.get(i).getUserName());
				ps.addBatch();
			}
			ps.executeBatch();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}
}
